<?php


class Lieux extends CI_Model
{

	private $lat;
	private $Lng;

	public static function findCordiante($address)
	{
	  //$this->error=false;
	     $host="maps.google.com";
	     $key=google_api_key;// recuperer le key du fichier de config
		
	    $Base_url = "http://".$host."/maps/geo?output=json&key=".$key;
	    $requette=$Base_url."&sensor=false&q=".urlencode($address);
		
		$contenu = file_get_contents($requette);
		$contenu = json_decode($contenu,true);
		$Lat = 0;
			$Lng = 0;
		if (isset($contenu["Placemark"])){
			$cordinate = $contenu["Placemark"][0]["Point"]["coordinates"];
			$Lat = $cordinate[1];
			$Lng = $cordinate[0];
		}

		$rt['lat']= $Lat;
		$rt['lng']= $Lng;
		return $rt;
		
	}
	public static function getLieu_by_type()
	{
		$row = array();
		//echo "sdf";
		if(isset($_POST['region'],$_POST['type_lieu']))
				{
					//echo "fdsfdsfsdfdsfdsf";
					
					
					$sql = 'SELECT website_lieu.id as id, website_lieu.nom as nom , website_lieu.descriptif as descriptif, website_lieu.position_lat as lat ,website_lieu.position_lon as lng , website_imagelieu.path_image as path  FROM  website_lieu left join website_imagelieu on website_imagelieu.lieu_id = website_lieu.id  left join  website_lieu_types on website_lieu.id = website_lieu_types.lieu_id left join website_type_lieu on website_lieu_types.type_lieu_id = website_type_lieu.id left join  website_ville on website_lieu.ville_id = website_ville.id left join website_departement on website_ville.departement_id = website_departement.id left join website_region on website_departement.region_id = website_region.id  WHERE website_region.nom = ? and website_type_lieu.id = ?';
					//echo $sql;
					$CI =& get_instance();	
					$query = $CI->db->query($sql,array($_POST['region'],$_POST['type_lieu']));
					$row = $query->result_array();
					
					

					$query->free_result();
				}
			
		return $row;

	}

	public static function getInfo($id)
	{
		$sql = 'SELECT website_lieu.id as id, website_lieu.nom as nom , website_lieu.descriptif as descriptif, website_lieu.position_lat as lat ,website_lieu.position_lon as lng , website_imagelieu.path_image as path  FROM  website_lieu left join website_imagelieu on website_imagelieu.lieu_id = website_lieu.id  left join  website_lieu_types on website_lieu.id = website_lieu_types.lieu_id left join website_type_lieu on website_lieu_types.type_lieu_id = website_type_lieu.id left join  website_ville on website_lieu.ville_id = website_ville.id left join website_departement on website_ville.departement_id = website_departement.id left join website_region on website_departement.region_id = website_region.id  WHERE website_lieu.id = ?';
					//echo $sql;
					$CI =& get_instance();	
					$query = $CI->db->query($sql,array($id));
					$row = $query->result_array();
					
					

					$query->free_result();
					return $row;
	}

	public static function getAllTypeLieu()
	{
		$row = array();
					$sql = 'SELECT * FROM  website_type_lieu ';
					
					$CI =& get_instance();	
					$query = $CI->db->query($sql);
					$row = $query->result_array();
					
					

					$query->free_result();
					return $row;
	}

	public static function ajax_getLieuByName()
	{
		$q = $_POST['name'];
		$sql = 'SELECT * FROM  website_lieu where website_lieu.nom like "'.$q.'%" limit 0,5';
					
					$CI =& get_instance();	
					$query = $CI->db->query($sql);
					$row = $query->result_array();
					
					

					$query->free_result();
					return $row;
	}

	public static function getType_lieu_visiteur()
	{
		$list_lieu = array();
		$CI =& get_instance();

		if($CI->session->userdata('visiteur_type'))
		{
			$sql ="select website_type_lieu.id as id  , website_type_lieu.nom as nom,website_type_lieu.path_image as path_image from website_type_visiteur left join website_type_visiteur_types_lieu on website_type_visiteur_types_lieu.type_visiteur_id = website_type_visiteur.id left join website_type_lieu  on website_type_lieu.id = website_type_visiteur_types_lieu.type_lieu_id  where website_type_visiteur.id = ?";
	
			$query = $CI->db->query($sql,array($CI->session->userdata('visiteur_type')));
			$row = $query->result_array();
			$list_event = $row;
			$query->free_result();
			$list_lieu = $row;
			
		}
		return $list_lieu;

	}

	public static function get_villes()
	{
		$sql = 'SELECT * FROM  website_ville ';
					
					$CI =& get_instance();	
					$query = $CI->db->query($sql);
					$row = $query->result_array();
					
					

					$query->free_result();
					return $row;
	}



}







?>